package com.itmiaojie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmiaojie.mapper.CategoryMapper;
import com.itmiaojie.pojo.Article;
import com.itmiaojie.pojo.Category;
import com.itmiaojie.pojo.ResponseResult;
import com.itmiaojie.pojo.dto.ArticleDto;
import com.itmiaojie.pojo.vo.*;
import com.itmiaojie.service.ArticleService;
import com.itmiaojie.mapper.ArticleMapper;
import com.itmiaojie.utils.BeanCopyUtils;
import com.itmiaojie.utils.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 *
 */
@Slf4j
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
    implements ArticleService{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisCache redisCache;
    /**
     * 返回 热门文章集合 前十条
     * @return
     */
    @Override
    public ResponseResult getHostArticleList() {
        //查询热门文章 封装成ResponseResult返回
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //必须文章是已经发表的
        queryWrapper.eq(Article::getStatus,0);
        //按照浏览量进行排序
        queryWrapper.orderByDesc(Article::getViewCount);
        //只显示十条记录
        Page<Article> page = new Page<>(1,10);
        IPage<Article> articleIPage = baseMapper.selectPage(page,queryWrapper);
        List<Article> records = articleIPage.getRecords();
        List<HotArticleVo> hotArticles = null;
        try {
            hotArticles = BeanCopyUtils.copyBeanList(records, HotArticleVo.class);
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.okResult(hotArticles);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) throws Exception {
        //必须是正式文章

        // 如果 有categoryId 就要 查询时要和传入的相同
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(categoryId) && categoryId > 0,Article::getCategoryId,categoryId);
        // 状态是正式发布的
        queryWrapper.eq(Article::getStatus,"0");
        //按照浏览量进行排序
        queryWrapper.orderByDesc(Article::getViewCount);
        //分页查询
        Page<Article> page = new Page<>(pageNum,pageSize);
        page = baseMapper.selectPage(page, queryWrapper);
        List<Article> records = page.getRecords();
        for (Article record : records) {
            Category category = categoryMapper.selectById(record.getCategoryId());
            record.setCategoryName(category.getName());
        }
        List<ArticleListVo> categoryVos = BeanCopyUtils.copyBeanList(page.getRecords(), ArticleListVo.class);

        PageVo pageVo = new PageVo(categoryVos,page.getTotal());


        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getArticleDetail(Long id) {
        Article article = baseMapper.selectById(id);
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        //根据分类id查询分类名
        Long categoryId = articleDetailVo.getCategoryId();
        Category category = categoryMapper.selectById(categoryId);
        if(category!=null){
            articleDetailVo.setCategoryName(category.getName());
        }
        //封装响应返回
        return ResponseResult.okResult(articleDetailVo);
    }

    @Override
    public ResponseResult updateViewCount(Long id) {
        String ida = String.valueOf(id);
        String count =  redisCache.getCacheObject(ida);
        redisCache.setCacheObject(id.toString(),String.valueOf(Integer.parseInt(count)+1));
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult insertArticle(ArticleDto articleDto) {
        Article article = BeanCopyUtils.copyBean(articleDto, Article.class);
        save(article);
        List<Long> tags = articleDto.getTags();
        articleMapper.insertTagsAndArticle(article.getId(),tags);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getPageCategoryList(Integer pageNum, Integer pageSize, String title, String summary) {
        IPage<Article> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(title),Article::getTitle,title);
        queryWrapper.eq(StringUtils.hasText(summary),Article::getSummary,summary);
        page(page,queryWrapper);
        return ResponseResult.okResult(new ArticlePageVo(page.getRecords(),page.getTotal()));
    }

    @Override
    public ResponseResult getArticleById(Long id) {
        Article article = getById(id);
        ArticleDto articleDto = BeanCopyUtils.copyBean(article, ArticleDto.class);
        List<Long> tags = articleMapper.selectTagIdByArticleId(id);
        articleDto.setTags(tags);
        return ResponseResult.okResult(articleDto);
    }

    @Override
    @Transactional
    public ResponseResult updateArticle(ArticleDto articleDto) {

        Article article = BeanCopyUtils.copyBean(articleDto, Article.class);
        updateById(article);
        List<Long> tags = articleDto.getTags();
        articleMapper.deleteArticleTagByIds(articleDto.getId());
        articleMapper.insertTagsAndArticle(articleDto.getId(),tags);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult deleteArticleById(Long id) {
        removeById(id);
        articleMapper.deleteArticleTagByIds(id);
        return ResponseResult.okResult();
    }

}




